package com.efesco.stat.sales;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.bstek.dorado.view.control.table.Column;
import com.bstek.dorado.view.control.table.ColumnGroup;
import com.bstek.dorado.view.control.table.Table;
import com.efesco.common.DBTools;
import com.efesco.common.Tools;
import com.efesco.finance.oughtReceive.OughtReceive;
import com.efesco.system.User;
import java.util.*;

/**
 * incomeViewModel
 */
public class incomeViewModel extends DefaultViewModel {
  protected void initControl(Control control)
      throws Exception {
    super.initControl(control);
    if (control.getId().equals("table1"))  {
    Table table1 = (Table)this.getControl("table1");
	SqlDataset productDataset = new SqlDataset();
    String sql="",foreign_enterprise_id="",fesc_user_id="";
    DoradoContext context = DoradoContext.getContext();
    Object obj = context.getAttribute(context.SESSION, "User");
    if (obj != null) {
    	User user = (User) obj;
		foreign_enterprise_id=user.getForeignEnterpriseId();
		fesc_user_id=user.getForEmployeeId();
		String startCmpDate="",endCmpDate="",startEmpDate="",endEmpDate="";
		if (! Tools.toStr(((String) context.getParameter("startCmpDate"))).equals("")) {
			startCmpDate = Tools.format(new Date((long)(Tools.toDouble((String) context.getParameter("startCmpDate")))),"yyyy-MM-dd");
		}
		if (! Tools.toStr(((String) context.getParameter("endCmpDate"))).equals("")) {
			endCmpDate = Tools.format(new Date((long)(Tools.toDouble((String) context.getParameter("endCmpDate")))),"yyyy-MM-dd");
		}
		if (! Tools.toStr(((String) context.getParameter("startEmpDate"))).equals("")) {
			startEmpDate = Tools.format(new Date((long)(Tools.toDouble((String) context.getParameter("startEmpDate")))),"yyyy-MM-dd");
		}
		if (! Tools.toStr(((String) context.getParameter("endEmpDate"))).equals("")) {
			endEmpDate = Tools.format(new Date((long)(Tools.toDouble((String) context.getParameter("endEmpDate")))),"yyyy-MM-dd");
		}
		String salesMan = Tools.toStr((String) context.getParameter("salesMan"));
    	String serviceMan = Tools.toStr((String) context.getParameter("serviceMan"));
    	if (!startCmpDate.equals(""))  {
	
    	sql="select distinct pt.product_id,pt.product_name,ptt.type_class " +
    			"from s_employee emp,s_single_company cmp,s_employee_on_off_post eoop,s_employee_contract_fee ecf,s_employee_contract_fee_item ecfi," +
    			"		s_company_businessman cbm,s_product pt,s_product_type ptt " +
    			"where emp.employee_id=eoop.employee_id and cmp.single_company_id=eoop.single_company_id " +
    			"and eoop.employee_on_off_id=ecf.employee_on_off_id and ecf.employee_contract_fee_id=ecfi.employee_contract_fee_id " +
    			"and ecfi.product_id=pt.product_id and pt.product_type_id=ptt.product_type_id and cmp.single_company_id=cbm.single_company_id " +
    			"and ptt.type_class>=4 and cmp.is_valid=1 " +
    			"and cmp.foreign_enterprise_id="+foreign_enterprise_id+
				" and exists " + DBTools.getRightSqlByBusinessman(fesc_user_id,"cbm") ;
		if (!startCmpDate.equals(""))  {
			sql = sql + " and cmp.input_date >= to_date('"+startCmpDate+"','yyyy-mm-dd')";
		}
		if (!endCmpDate.equals(""))  {
			sql = sql + " and cmp.input_date <= to_date('"+endCmpDate+"','yyyy-mm-dd')";
		}
		if (!salesMan.equals(""))  {
			sql = sql + " and cbm.salesman_id = "+salesMan;
		}
		if (!serviceMan.equals(""))  {
			sql = sql + " and cbm.businessman_id = "+serviceMan;
		}
		if (!startEmpDate.equals(""))  {
			sql = sql + " and ecf.apply_date >= to_date('"+startEmpDate+"','yyyy-mm-dd')";
		}
		if (!endEmpDate.equals(""))  {
			sql = sql + " and ecf.apply_date <= to_date('"+endEmpDate+"','yyyy-mm-dd')";
		}
		
		sql=sql + " order by ptt.type_class desc,pt.product_name ";
		productDataset.setSql(sql);
		productDataset.setPageSize(0);
		productDataset.load(true);
		productDataset.moveFirst();

    	while (!productDataset.isLast()) {
   			table1.addColumn(productDataset.getString("product_id"));
    		productDataset.moveNext();
    	}
    	table1.addColumn("colSum");
   	
    	}
    }
    }

  }
  
  protected void initDataset(ViewDataset dataset) 
  throws Exception {

	  super.initDataset(dataset);
	  if (dataset.getId().equals("incomeDetailDataset"))  {
	    	DoradoContext context = DoradoContext.getContext();
	    	if (!Tools.toStr(((String) context.getParameter("startCmpDate"))).equals(""))  {
	    		dataset.setAutoLoadData(true);
	    	}
	  }
	  
	  if (dataset.getId().equals("invoiceDataset"))  {
	    	DoradoContext context = DoradoContext.getContext();
	    	if (!Tools.toStr(((String) context.getParameter("startCmpDate"))).equals(""))  {
	    		dataset.setAutoLoadData(true);
	    	}
	  }

  }


}
